import requests
import json
class BaiDu(object):
    def __init__(self, word):
        self.url = 'https://fanyi.baidu.com/sug'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
        }
        self.data = {
            'kw': word
        }
    def get_data(self):
        response = requests.post(self.url, headers=self.headers, timeout=3, data=self.data)
        return response.content

    def analyse_data(self, data):
        dict_data = json.loads(data)
        # try:
        print(dict_data["data"][0]["v"])
    def run(self):
        response = self.get_data()
        self.analyse_data(response)
if __name__ == '__main__':
    while (1):
        try:
            word = input("请输入需要翻译的词/句子：")
            translate = BaiDu(word)
            translate.run()
        except:
            print("找不到，请重新输入")